

using System;
using System.Collections.Generic;
using System.Linq;
using BusinessLogic.Caching;
using BusinessLogic.Configuration.Settings;
using BusinessLogic.Data;
using BusinessLogic.Infrastructure;
using Common.Utils;

namespace BusinessLogic.Directory
{
    /// <summary>
    /// Language service
    /// </summary>
    public partial class LanguageService : ILanguageService
    {
        #region Constants
        private const string LANGUAGES_ALL_KEY = "Nop.language.all-{0}";
        private const string LANGUAGES_BY_ID_KEY = "Nop.language.id-{0}";
        private const string LANGUAGES_PATTERN_KEY = "Nop.language.";
        #endregion

        #region Fields

        /// <summary>
        /// Object context
        /// </summary>
        private readonly DataMailDataContext m_context;

        /// <summary>
        /// Cache manager
        /// </summary>
        private readonly ICacheManager _cacheManager;

        #endregion

        #region Ctor

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="context">Object context</param>
        public LanguageService(DataMailDataContext context)
        {
            this.m_context = context;
            this._cacheManager = new PayrollRequestCache();
        }

        #endregion

        #region Methods

        /// <summary>
        /// Deletes a language
        /// </summary>
        /// <param name="languageId">Language identifier</param>
        public void DeleteLanguage(int languageId)
        {
            var language = GetLanguageById(languageId);
            if (language == null)
                return;

            
            //if (!m_context.IsAttached(language))
            //    m_context.Languages.Attach(language);
            //m_context.DeleteObject(language);
            //m_context.SaveChanges();

            if (this.CacheEnabled)
            {
                _cacheManager.RemoveByPattern(LANGUAGES_PATTERN_KEY);
            }
        }

        /// <summary>
        /// Gets all languages
        /// </summary>
        /// <returns>Language collection</returns>
        public List<Language> GetAllLanguages()
        {
            //bool showHidden = PayrollContext.Current.IsAdmin;
            return GetAllLanguages(true);
        }

        /// <summary>
        /// Gets all languages
        /// </summary>
        /// <param name="showHidden">A value indicating whether to show hidden records</param>
        /// <returns>Language collection</returns>
        public List<Language> GetAllLanguages(bool showHidden)
        {
            string key = string.Format(LANGUAGES_ALL_KEY, showHidden);
            object obj2 = _cacheManager.Get(key);
            if (this.CacheEnabled && (obj2 != null))
            {
                return (List<Language>)obj2;
            }


            var query = from l in m_context.Languages
                        orderby l.DisplayOrder
                        where showHidden || l.Published
                        select new Language {  LanguageId = l.LanguageId, LanguageCulture = l.LanguageCulture, Name = l.Name, DisplayOrder = l.DisplayOrder
                            , FlagImageFileName = l.FlagImageFileName, Published = l.Published
                        };
            var languages = query.ToList();

            if (this.CacheEnabled)
            {
                _cacheManager.Add(key, languages);
            }
            return languages;
        }

        /// <summary>
        /// Gets a language
        /// </summary>
        /// <param name="languageId">Language identifier</param>
        /// <returns>Language</returns>
        public Language GetLanguageById(int languageId = 7)
        {
            if (languageId == 0)
                return null;

            string key = string.Format(LANGUAGES_BY_ID_KEY, languageId);
            object obj2 = _cacheManager.Get(key);
            if (this.CacheEnabled && (obj2 != null))
            {
                return (Language)obj2;
            }


            var query = from l in m_context.Languages
                        where l.LanguageId == languageId
                        select new Language { Published = l.Published, FlagImageFileName=l.FlagImageFileName, DisplayOrder =l.DisplayOrder, Name = l.Name
                            , LanguageCulture = l.LanguageCulture, LanguageId = l.LanguageId
                        };
            var language = query.SingleOrDefault();

            if (this.CacheEnabled)
            {
                _cacheManager.Add(key, language);
            }
            return language;
        }

        /// <summary>
        /// Inserts a language
        /// </summary>
        /// <param name="language">Language</param>
        public void InsertLanguage(Language language)
        {
            if (language == null)
                throw new ArgumentNullException("language");
            
            language.Name = CommonHelper.EnsureNotNull(language.Name);
            language.Name = CommonHelper.EnsureMaximumLength(language.Name, 100);
            language.LanguageCulture = CommonHelper.EnsureNotNull(language.LanguageCulture);
            language.LanguageCulture = CommonHelper.EnsureMaximumLength(language.LanguageCulture, 20);
            language.FlagImageFileName = CommonHelper.EnsureNotNull(language.FlagImageFileName);
            language.FlagImageFileName = CommonHelper.EnsureMaximumLength(language.FlagImageFileName, 50);

            

            //m_context.Languages.AddObject(language);
            //m_context.SubmitChanges();

            if (this.CacheEnabled)
            {
                _cacheManager.RemoveByPattern(LANGUAGES_PATTERN_KEY);
            }
        }

        /// <summary>
        /// Updates a language
        /// </summary>
        /// <param name="language">Language</param>
        public void UpdateLanguage(Language language)
        {
            if (language == null)
                throw new ArgumentNullException("language");

            language.Name = CommonHelper.EnsureNotNull(language.Name);
            language.Name = CommonHelper.EnsureMaximumLength(language.Name, 100);
            language.LanguageCulture = CommonHelper.EnsureNotNull(language.LanguageCulture);
            language.LanguageCulture = CommonHelper.EnsureMaximumLength(language.LanguageCulture, 20);
            language.FlagImageFileName = CommonHelper.EnsureNotNull(language.FlagImageFileName);
            language.FlagImageFileName = CommonHelper.EnsureMaximumLength(language.FlagImageFileName, 50);

            
            //if (!m_context.IsAttached(language))
            //    m_context.Languages.Attach(language);

            //m_context.SaveChanges();

            if (this.CacheEnabled)
            {
                _cacheManager.RemoveByPattern(LANGUAGES_PATTERN_KEY);
            }
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets a value indicating whether cache is enabled
        /// </summary>
        public bool CacheEnabled
        {
            get
            {
                return IoC.Resolve<ISettingManager>().GetSettingValueBoolean("Cache.LanguageManager.CacheEnabled");
            }
        }

        #endregion
    }
}
